package com.bootdo.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.time.temporal.Temporal;
import java.util.*;

public class DateUtils {

    public static String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return  sdf.format(date);
    }


    public static Date parse(String format1) throws ParseException {
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = df1.parse(format1);
        return  parse;
    }

    public static String getYear() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        return  sdf.format(new Date());
    }

    public static String getMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return  sdf.format(new Date());
    }

    public static String getMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM");
        return  sdf.format(date);
    }

    // 日期转化为 2022-03-04 转化为  2022.04
    public static String getYearPointMonth(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
        return  sdf.format(date);
    }

    // 日期转化为 2022-03-04  2022-03-05  转化为  2022.05-05
    public static String getYearPointYearMonth(Date startDate, Date endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
        SimpleDateFormat sdf1 = new SimpleDateFormat("MM");
        String start = sdf.format(startDate);
        String end = sdf1.format(endDate);
        return start + "-" + end;
    }


    /**
     * 计算两个时间差 返回时间yyyy-MM
     * @param startTime
     * @param endTime
     * @return
     */
    public static List<Date> calSubectDate(Date startTime, Date endTime) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-01");
        String start3 = sdf.format(startTime);
        System.out.println("start3=" + start3);

        List<Date> dates = new ArrayList<>();
        String start = format(startTime);

        String end = format(endTime);
        // 计算相差的时间
//        LocalDate start1 = LocalDate.parse(start);
        // 存入list的时间
        LocalDate start4 = LocalDate.parse(start3);
        Temporal temporal1 = LocalDate.parse(start3);
        Temporal temporal2 = LocalDate.parse(end);
        // 方法返回为相差月份
        long month = ChronoUnit.MONTHS.between(temporal1, temporal2);
        long year = ChronoUnit.YEARS.between(temporal1, temporal2);
        System.out.println("month=" + month);
        System.out.println("year=" + year);
        if (year != 0) {
            month = month * year;
        }
        LocalDate start2 = null;
        for (long i = 0; i <= month; i++) {
            start2 = start4.plusMonths(i);
            Date date = Date.from(start2.atStartOfDay(ZoneId.systemDefault()).toInstant());
            String format = format(date);
//            System.out.println("date==" + date);
            System.out.println("format==" + format);
            dates.add(date);
        }

        //当前时间

        return dates;
    }

        public static void main(String[] args) throws ParseException {
            List<Date> dates = calSubectDate(DateUtils.parse("2022-09-30"), DateUtils.parse("2023-02-15"));
            System.out.println("dates==" + dates);

//            Date date = calCmComPlanStartDate(new Date(), 26);
//            System.out.println("date=" + date);
//
//            DateUtils test = new DateUtils();
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//
//            long l = calSubect(new Date(), new Date("2022-10-01 00:00:00"));
//            System.out.println("l----" + l);

            // testGetDateStr();

//		System.out.println(CommonDateParseUtil.date2string(
//				firstDayOfWeek(new Date()), CommonDateParseUtil.YYYY_MM_DD));
//		System.out.println(CommonDateParseUtil.date2string(
//				lastDayOfWeek(new Date()), CommonDateParseUtil.YYYY_MM_DD));


            // Date
//            Date currentDate = new Date();
//            System.out.println("当前日期是：" + df.format(currentDate));
//            System.out.println("一周后的日期是：" + df.format(test.nextWeek(currentDate)));
//            System.out.println("一月后的日期是：" + df.format(test.nextMonth(currentDate)));
//            System.out.println("一年后的日期是：" + df.format(test.nextYear(currentDate)));
//            // Timestamp
//            Timestamp currentTime = new Timestamp(System.currentTimeMillis());
//            System.out.println("当前日期是：" + df.format(currentTime));
//            System.out.println("一周后的日期是：" + df.format(test.nextWeek(currentTime)));
//            System.out.println("一月后的日期是：" + df.format(test.nextMonth(currentTime)));
//            System.out.println("一年后的日期是：" + df.format(test.nextYear(currentTime)));
//            // 另一种计算方式，这种方式计算月和年的日期比较困难
//            Timestamp nextTime = new Timestamp(currentTime.getTime() + 7 * 24 * 60
//                    * 60 * 1000);
//            System.out.println("当前日期是：" + df.format(currentTime));
//            System.out.println("一周后的日期是：" + df.format(nextTime));
//            System.out.println(lastDayOfMonth());
        }


        // 获取下一周的日期
        public static Date nextWeek(Date currentDate) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(currentDate);
            cal.add(GregorianCalendar.DATE, 7);// 在日期上加7天
            return cal.getTime();
        }

        // 获取本周日的日期
        public static Date getSunday(Date monday) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(monday);
            cal.add(GregorianCalendar.DATE, 6);// 在日期上加6天
            return cal.getTime();
        }

        // 获取下一月的日期
        public static Date nextMonth(Date currentDate) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(currentDate);
            cal.add(GregorianCalendar.MONTH, 1);// 在月份上加1
            return cal.getTime();
        }

    // 获取下一月的日期
    public static Date next3Month(Date currentDate) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(currentDate);
        cal.add(GregorianCalendar.MONTH, 3);// 在月份上加1
        return cal.getTime();
    }

    // 获取某个时间的几个月后
    public static Date nextXMonth(Date currentDate, int x) {
        GregorianCalendar cal = new GregorianCalendar();
        cal.setTime(currentDate);
        cal.add(GregorianCalendar.MONTH, x);// 在月份上加1
        return cal.getTime();
    }

    /**
     * 计算两个时间差 返回时间yyyy-MM
     * @param startTime
     * @param endTime
     * @return
     */
    public static long calSubect(Date startTime, Date endTime) {
        String start = format(startTime);
        String end = format(endTime);
        Temporal temporal1 = LocalDate.parse(start);
        Temporal temporal2 = LocalDate.parse(end);
        // 方法返回为相差月份
        long ll = ChronoUnit.MONTHS.between(temporal1, temporal2);
        return ll;
    }

    /**
     * 计算 当前时间+1月
     * @param startDate
     * @param i
     * @return
     */
    public static Date calCmComPlanStartDate(Date startDate, int i) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("MM");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy");
        SimpleDateFormat df3 = new SimpleDateFormat("dd");
        String format = df.format(startDate);
        String format2 = df2.format(startDate);
        String format3 = df3.format(startDate);
        System.out.println("form==" + format);
        int i1 = Integer.parseInt(format) + i;
        int i2 = i1 % 12;
        int i3 = i1 / 12;
        String month = "";
        String year = Integer.parseInt(format2) + i3 + "";
        if (i1 < 10) {
            month = "0" + i2;
        }else {
            month = i2 + "";
        }
        System.out.println("i1=" + i2);
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = year + "-" + month + "-" + format3;
        System.out.println("for1==" + format1);
        Date parse = df1.parse(format1);
        return parse;
    }


    /**
     * 计算 当前时间+1月
     * @param startDate
     * @param i
     * @return
     */
    public static String calCmComPlanStartDateMonth(Date startDate) throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("MM");
        String format = df.format(startDate);
        return format;
    }

    public static String formatExport(Date planInvestDate) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        String format = df.format(planInvestDate);
        return format;
    }


    // 获取下一年的日期
        public Date nextYear(Date currentDate) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(currentDate);
            cal.add(GregorianCalendar.YEAR, 1);// 在年上加1
            return cal.getTime();
        }

        /**
         * @param date
         * @return
         * @作者 王建明
         * @创建日期 2013-06-06
         * @创建时间 10:47:36
         * @描述 —— 获取日期为该月的第几周
         */
        public static int getWeekInMonthNum(Date date) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(date);
            int week = cal.get(Calendar.WEEK_OF_MONTH);// 获取是本月的第几周
            return week;
        }

        /**
         * @作者 王建明
         * @创建日期 2013-01-24
         * @创建时间 08:43:41
         * @描述 —— 获取当前日期的本周第一天
         */
//        public static Date firstDayOfWeek(Date date) {
//            int year = Integer.parseInt(CommonDateParseUtil.getNowYear());
//            int month = Integer.parseInt(CommonDateParseUtil.getNowMonth());
//            int day = Integer.parseInt(CommonDateParseUtil.getNowDay());
//
//            // 取本周第一天 最后一天
//            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.YEAR, year);
//            calendar.set(Calendar.MONTH, month - 1);
//            calendar.set(Calendar.DAY_OF_MONTH, day);
//            calendar.getTime();
//            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
//
//            return calendar.getTime();
//        }

        /**
         * @param date
         * @return
         * @作者 王建明
         * @创建日期 2013-01-24
         * @创建时间 08:48:03
         * @描述 —— 获取本周最后一天
         */
//        public static Date lastDayOfWeek(Date date) {
//            int year = Integer.parseInt(CommonDateParseUtil.getNowYear());
//            int month = Integer.parseInt(CommonDateParseUtil.getNowMonth());
//            int day = Integer.parseInt(CommonDateParseUtil.getNowDay());
//



//            // 取本周最后一天
//            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.YEAR, year);
//            calendar.set(Calendar.MONTH, month - 1);
//            calendar.set(Calendar.DAY_OF_MONTH, day);
//            calendar.getTime();
//            calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
//
//            return getSunday(calendar.getTime());
//        }

        /**
         * @return
         * @作者 王建明
         * @创建日期 2013-01-24
         * @创建时间 08:53:51
         * @描述 —— 获取本月第一天
         */
        public static String firstDayOfMonth() {
            Calendar cal = Calendar.getInstance();

            SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
            cal.set(Calendar.DATE, 1);
            cal.roll(Calendar.DATE, -1);
            // 当前月的第一天
            cal.set(GregorianCalendar.DAY_OF_MONTH, 1);
            Date beginTime = cal.getTime();
            String beginTime1 = datef.format(beginTime) + " 00:00:00";
            return beginTime1;
        }

        /**
         * @return
         * @作者 王建明
         * @创建日期 2013-01-24
         * @创建时间 08:54:06
         * @描述 —— 获取本月最后一天
         */
        public static String lastDayOfMonth() {
            Calendar cal = Calendar.getInstance();

            SimpleDateFormat datef = new SimpleDateFormat("yyyy-MM-dd");
            // 当前月的最后一天
            cal.set(Calendar.DATE, 1);
            cal.roll(Calendar.DATE, -1);
            Date endTime = cal.getTime();
            String endTime1 = datef.format(endTime) + " 23:59:59";
            return endTime1;
        }

        /**
         * @return
         * @作者 王建明
         * @创建日期 2013-05-22
         * @创建时间 09:48:04
         * @描述 —— 获取当前星期数字
         */
        public static int getNowWeekNum() {
            int weekDay = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
            return weekDay == 1 ? 7 : weekDay - 1;
        }

        /**
         * @return
         * @作者 王建明
         * @创建日期 2013-05-22
         * @创建时间 16:09:29
         * @描述 —— 根据星期和时刻获取当前周的相应时间日期
         */
//        public static Date getNowWeekDate(int week, int time) {
//            int mondayPlus = getMondayPlus() + (week - 1);
//            GregorianCalendar currentDate = new GregorianCalendar();
//            currentDate.add(GregorianCalendar.DATE, mondayPlus);
//            Date date = currentDate.getTime();
//            String dateStr = CommonDateParseUtil.date2string(date,
//                    CommonDateParseUtil.YYYY_MM_DD);
//            if (time < 10)
//                dateStr += (" 0" + time + ":00:00");
//            else
//                dateStr += (" " + time + ":00:00");
//            // return preMonday;
//            return CommonDateParseUtil.string2date(dateStr,
//                    CommonDateParseUtil.YYYY_MM_DD_HH_MM_SS);
//        }

        // 获得当前日期与本周一相差的天数
        private static int getMondayPlus() {
            Calendar cd = Calendar.getInstance();
            // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
            int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
            if (dayOfWeek == 1) {
                return -6;
            } else {
                return 2 - dayOfWeek;
            }
        }

        /**
         * @param currentDate
         * @param num
         * @return
         * @作者 王建明
         * @创建日期 2013-6-8
         * @创建时间 下午2:44:24
         * @描述 —— 获取与指定日期距离num天的日期
         */
        public static Date getDateFromSourceDate(Date currentDate, int num) {
            GregorianCalendar cal = new GregorianCalendar();
            cal.setTime(currentDate);
            cal.add(GregorianCalendar.DATE, num);
            return cal.getTime();
        }

        /**
         * @param date1
         * @param date2
         * @return
         * @作者 王建明
         * @创建日期 2013-06-20
         * @创建时间 14:08:37
         * @描述 —— 计算两个日期相隔多少天
         */
        public static long differ(Date date1, Date date2) {
            Long differLong = date2.getTime() / 86400000 - date1.getTime()
                    / 86400000; // 用立即数，减少乘法计算的开销
            return Math.abs(differLong);
        }

}
